﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using WebView.WSSite;
using WebView.WSPagina;

namespace WebView.Pagina {
	public partial class Default : System.Web.UI.Page {
		
		private PaginasServices t;
		private SitesServices s;
		
		protected void Page_Load(object sender, EventArgs e) {
			s = new SitesServices();
			t = new PaginasServices();
			this.refreshList();
//			this.refreshGrid();
		}

		protected void Button1_Click(object sender, EventArgs e) {
			Label3.Text = "Adicionar";
			HiddenField1.Value = null;
			Panel1.Visible = false;
			Panel2.Visible = true;
		}

		protected void Button2_Click(object sender, EventArgs e) {
			uint id = uint.Parse(GridView1.SelectedDataKey.Value.ToString());
			t.remover(id);
			this.refreshGrid();
		}

		protected void Button3_Click(object sender, EventArgs e) {
			if (String.IsNullOrEmpty(HiddenField1.Value))
				t.adicionar(TextBoxNome.Text, TextBoxDescricao.Text, TextBoxPalavraChave.Text, TextBoxAutor.Text, DateTime.Now, uint.Parse(TextBoxTemplate.Text));
			//else
			//	t.atualizar(TextBoxNome.Text, TextBoxDescricao.Text, TextBoxAutor.Text, TextBoxData.Text, TextBoxTemplate.Text);
			Panel1.Visible = true;
			Panel2.Visible = false;
			this.refreshGrid();
		}

		protected void Button4_Click(object sender, EventArgs e) {
			Panel1.Visible = true;
			Panel2.Visible = false;
		}

		protected void Button5_Click(object sender, EventArgs e) {
			Label3.Text = "Editar";
			uint id = uint.Parse(GridView1.SelectedDataKey.Value.ToString());
//			WebView.WSPagina.Pagina o = t.get(id);
			HiddenField1.Value = id.ToString();
//			TextBoxNome.Text = o.nome;
//			TextBoxDescricao.Text = o.descricao;
//			TextBoxAutor.Text = o.autor;
//			TextBoxData.Text = o.data;
//			TextBoxTemplate.Text = o.template;
			Panel1.Visible = false;
			Panel2.Visible = true;
			this.refreshGrid();
		}

		protected void Button6_Click(object sender, EventArgs e) {
			uint id = uint.Parse(GridView1.SelectedDataKey.Value.ToString());
			//t.duplicar(id);			
			this.refreshGrid();
		}

		protected void Button7_Click(object sender, EventArgs e) {
			//Importar
		}

		protected void Button8_Click(object sender, EventArgs e) {
			//Exportar
		}

		protected void Button9_Click(object sender, EventArgs e) {
			
		}

		private void refreshList() {
			DataList1.DataSource = s.listar();                  
			DataList1.DataBind();
			
		}

		private void refreshGrid() {
			GridView1.DataSource = t.listar();
			GridView1.DataBind();
		}

	}
}
